package Frame.battleanimation;

import FCRendering.FCRMaster;
import VWorldGameStudio.FCAnimation;

import java.awt.*;
import java.util.ArrayList;

import static FCRendering.FCRMaster.animationLib;
import static FCRendering.FCRMaster.fcv;

/**
 * bpbwan
 * 回旋镖特定的
 */
public class AttackHuiXuanBiaoAni extends ABattleAni implements IBattleAni {

    ArrayList<ChildAni> childAnis = new ArrayList<>();
    private int oneChildFireTime = 12;

    protected FCAnimation animation;

    public AttackHuiXuanBiaoAni(int[] startPoint, int[] endpoint, boolean isPlayerAttackMode) {
        animation = animationLib.getAnimationOfID("huiXuanBiao");
        if (isPlayerAttackMode) {
            playerOffset[0] = (int) (20 * fcv.getProportion());
            startPoint[0] -= playerOffset[0];
        } else {
            playerOffset[0] = (int) (40 * fcv.getProportion());
            startPoint[0] += playerOffset[0];
        }
        for (int i = 0; i < endpoint.length / 2; i++) {
            int[] endp = new int[2];
            endp[0] = endpoint[i * 2];
            endp[1] = endpoint[i * 2 + 1];
            int[] start = new int[2];
            start[0] = startPoint[0];
            start[1] = startPoint[1];
            ChildAni c = new ChildAni(animation, start, endp, isPlayerAttackMode, this, oneChildFireTime * i);
            childAnis.add(c);
        }
        //拔刀
        allFrames = childAnis.get(0).allFrames + oneChildFireTime * (childAnis.size() - 1);
    }


    public void nextFrame(int frame) {
        ChildAni lastOne = null;
        for (ChildAni childAni : childAnis) {
            if (childAni.whenStartFrame <= frame) {
                childAni.nextFrame(frame - childAni.whenStartFrame);
            }
            lastOne = childAni;
        }
        //TODO 收刀动作
        if (frame > lastOne.whenStartFrame + oneChildFireTime) {
            attackReadyState = true;
        }
    }

    @Override
    public void paintBattleSceen(Graphics g, FCRMaster fcrMaster) {
        for (ChildAni childAni : childAnis) {
            childAni.paintBattleSceen(g, fcrMaster);
        }
    }


    @Override
    public void fire(Graphics g, FCRMaster fcrMaster) {
    }

    @Override
    public void flying(Graphics g, FCRMaster fcrMaster) {
    }

    @Override
    public void boom(Graphics g, FCRMaster fcrMaster) {
    }


    protected class ChildAni extends ABattleAni {
        //原点
        protected int[] startPoint;
        //结束点
        protected int[] endPoints;
        protected FCAnimation animation;

        protected int flyFrame1 = 0;
        protected int flyFrame2 = 0;
        protected int flyFrame3 = 0;

        protected int[] paintPoints = new int[2];

        protected int boomPngIndex = -1;
        protected double targetDistance;
        private int endFrame = 5;
        boolean hited = true;//打中的
        ABattleAni parent;
        protected int whenStartFrame = 0;
        protected int insideCountFrame = 0;
        protected int[] point1 = new int[2];
        protected int[] point2 = new int[2];
        protected int[] point3 = new int[2];
        protected int[] point4 = new int[2];
        protected int allBoomSize = 0;

        public ChildAni(FCAnimation ani, int[] startPoint, int[] endpoint, boolean isPlayerAttackMode, ABattleAni parent, int whenStartFrame) {
            this.parent = parent;
            this.animation = ani;
            this.startPoint = startPoint;
            this.endPoints = endpoint;
            this.whenStartFrame = whenStartFrame;
            if (endpoint[0] <= 0 || endpoint[0] >= (int) (1920 * fcv.getProportion())) {
                hited = false;//没打着
            }
            this.isPlayerAttackMode = isPlayerAttackMode;
            //分3段
            //1段，手中飞出
            //2段，横扫怪物
            //3段，回旋镖回到手中
            int v = (int) (560 * fcv.getProportion());
            point1 = new int[]{(int) (900 * fcv.getProportion()), (int) (-700 * fcv.getProportion())};
            targetDistance = AnimationUtil.getDistance(startPoint, point1);
            flyFrame1 = (int) (16 * targetDistance / v);
            point2 = new int[]{endpoint[0], (int) (-700 * fcv.getProportion())};
            point3 = new int[]{endpoint[0], (int) (1500 * fcv.getProportion())};
            targetDistance = AnimationUtil.getDistance(point2, point3);
            flyFrame2 = (int) (16 * targetDistance / v);
            point4 = new int[]{(int) (900 * fcv.getProportion()), (int) (1500 * fcv.getProportion())};
            targetDistance = AnimationUtil.getDistance(point4, startPoint);
            flyFrame3 = (int) (16 * targetDistance / v);
            allBoomSize = ani.getBoomPng().size();
            this.allFrames = flyFrame1 + flyFrame2 + flyFrame3 + endFrame;
        }

        private void getNextBoomIndex(int frame) {
            if(frame%6==0) {
                if (boomPngIndex == allBoomSize - 1) {
                    boomPngIndex = 0;
                } else {
                    boomPngIndex++;
                }
            }
        }

        @Override
        public void nextFrame(int frame) {
            insideCountFrame++;
            if (frame < flyFrame1) {
                AnimationUtil.getAnimatePoint(paintPoints, startPoint, point1, (float) frame / flyFrame1);
                paintPoints[0] = paintPoints[0] - (int) (60 * fcv.getProportion());
                paintPoints[1] = paintPoints[1] - (int) (75 * fcv.getProportion());
                getNextBoomIndex(frame);
            } else if (frame < (flyFrame2 + flyFrame1)) {
                int s = frame - flyFrame1;
                AnimationUtil.getAnimatePoint(paintPoints, point2, point3, (float) s / flyFrame2);
                paintPoints[0] = paintPoints[0] - (int) (75 * fcv.getProportion());
                paintPoints[1] = paintPoints[1] - (int) (75 * fcv.getProportion());
                getNextBoomIndex(frame);
            } else if (frame < (flyFrame2 + flyFrame1 + flyFrame3)) {
                int s = frame - flyFrame1 - flyFrame2;
                AnimationUtil.getAnimatePoint(paintPoints, point4, startPoint, (float) s / flyFrame3);
                paintPoints[0] = paintPoints[0] - (int) (60 * fcv.getProportion());
                paintPoints[1] = paintPoints[1] - (int) (75 * fcv.getProportion());
                getNextBoomIndex(frame);
            } else {
                boomPngIndex = -1;
            }
        }

        @Override
        public void paintBattleSceen(Graphics g, FCRMaster fcrMaster) {
            if (boomPngIndex != -1) {
                g.drawImage(animation.getBoomPng().get(boomPngIndex), paintPoints[0], paintPoints[1],
                        (int) (animation.getBoomPngScales() * animation.boomPngSizeW * fcv.getProportion()),
                        (int) (animation.getBoomPngScales() * animation.boomPngSizeH * fcv.getProportion()), fcrMaster);
            }
        }
    }
}
